# -*- coding: utf-8 -*-
from urllib2 import Request, urlopen
from urllib import quote
from urlparse import urljoin

import os, sys, re
COMMON_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__),'..'))
sys.path.append(COMMON_DIR)
from BeautifulSoup import BeautifulSoup
from settings import category_image_group
url_template = 'http://www.xiaoli.cc/category/%s'
video_image_obj = re.compile(r'div\sstyle="background:#fff;padding:10px;margin-bottom:10px;">\s+<img\ssrc="(.+?)"\s+style="display:block;width:190px;"\s+/>', flags= re.DOTALL)

def url_read(url, time = 5):
    try:
        return urlopen(url).read()
    except:
        if time > 0:
            return url_read(url, time - 1)
        else:
            raise
        
def download_cover(cate, page_url):
    ext = '.jpg'
    id = page_url.split('/')[-1]
    download_path = os.path.abspath(os.path.join(COMMON_DIR, '..', 'upload', category_image_group[cate], "%s%s" %(id, ext), ))
    print download_path
    if not os.path.exists(download_path):
        content = url_read(page_url)
        video_image_m = video_image_obj.search(content)
        if video_image_m:
            video_image = video_image_m.group(1)
            url_path = urljoin(url_template, quote(video_image))
            headers = {'Referer':'http://www.xiaoli.cc/video/detail/%s' % id,
                       'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1',
                       }
            url_path_request = Request(url_path, headers = headers)
            result = urlopen(url_path_request)
            download_dir = os.path.dirname(download_path)
            if not os.path.exists(download_dir):
                os.makedirs(download_dir)
            f = open(download_path,'wb')
            f.write(result.read())
            f.close()
        else:
            if len(content) == 941:
                print 'retry download video_image'
                download_cover(cate, page_url)
            else:
                print 'no video_image'
    else:
        print 'already exist'

     
def main():
    cates = []
    for i in range(1,12):
        url = url_template % i
        print url
        content = url_read(url)
        soup = BeautifulSoup(content)
        rows = soup.find('table', attrs = {'id':'categoryTable'}).findAll('tr')[1:]
        print len(rows)
        task_urls = []
        for row in rows:
            task_url = urljoin(url_template,row.a['href'])
            task_urls.append(task_url)
        cates.append((i, task_urls))
        
    total_length = sum(len(c[1]) for c in cates)
    count = 0
    for cate,task_urls in cates:
        length = len(task_urls)
        for i,task_url in enumerate(task_urls):
            print '%s/%s  : %s/%s' % (i, length, count, total_length)
            download_cover(cate, task_url)
            count = count + 1
            
            
if __name__ == '__main__':
    main()